# 导入图片资源
from imageResource import imageResource


# 编写敌人类
class Enemy:
    def __init__(self, x, y):
        # 大小
        self.size = 80
        # 速度
        self.speed = 2
        # 位置
        self.x = x
        self.y = y
        # 图片
        self.img = imageResource.getEnemyImg()
        # id
        self.id = None
        # 当前敌机是否死亡的标志 - 死亡 True - 未死亡 False
        self.isDie = False

    # 移动的方法
    def move(self, keys=None):
        self.y += self.speed

    # 显示的方法
    def show(self, canvas):
        canvas.show(self)

    # 是否需要清除
    def isClear(self, winWidth, winHeight):
        return (self.y > winHeight + self.size) or (self.isDie)

    # 是否产生碰撞的逻辑 - 如果碰撞 返回True 如果不碰撞 返回False
    def isCollision(self, obj):
        # Ax = self.x - self.size // 2
        # Ay = self.y - self.size // 2
        # Asize = self.size
        # Bx = obj.x - obj.size // 2
        # By = obj.y - obj.size // 2
        # Bsize = obj.size
        Asize = self.size // 2
        Bsize = obj.size // 2
        Ax = self.x - Asize // 2
        Ay = self.y - Asize // 2
        Bx = obj.x - Bsize // 2
        By = obj.y - Bsize // 2

        t1 = Ax + Asize < Bx
        t2 = Ax > Bx + Bsize
        t3 = Ay + Asize < By
        t4 = Ay > By + Bsize

        return not (t1 or t2 or t3 or t4)


# 敌人工厂类
class EnemyFactory:
    def __init__(self):
        self.list = []

    # 创建敌人
    def create(self, x, y):
        obj = Enemy(x, y)
        self.list.append(obj)
        return obj
    
    # 清理超出屏幕的子弹
    def flush(self, winWidth, winHeight):
        i = 0
        while i < len(self.list):
            if self.list[i].isClear(winWidth, winHeight):
                self.list.pop(i)
            else:
                i += 1


enemyFactory = EnemyFactory()